<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use app\admin\model\Order;

/**
 * @ControllerAnnotation(title="user_info")
 */
class UserInfo extends AdminController {

    use \app\admin\traits\Curd;

    public function __construct(App $app) {
        parent::__construct($app);

        $this->model = new \app\admin\model\UserInfo();

    }


    /**
     * @NodeAnotation(title="列表")
     */
    public function index() {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();

            if (input('uid') > 0) {
                $where['uid'] = input('uid');
            }
            $count = $this->model->where($where)->count();
            $list  = $this->model->where($where)->page($page, $limit)->order($this->sort)->select()->toArray();
            foreach ($list as $k => $v) {
                $coin_type           = config('cointype');
                $list[$k]['company'] = $coin_type[$v['type']];
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('uid', input('id'));
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add() {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('uid', input('uid'));
        return $this->fetch();
    }

    /**
     *获取所有货币类型
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/8 下午12:34
     */
    public function getCoinType() {
        $config = config('cointype');
        $data   = [];
        $i      = 0;
        foreach ($config as $key => $value) {
            $data[$i]['id']    = $key;
            $data[$i]['title'] = $value;
            $i++;
        }
        $this->success('成功', $data);
    }
}